package com.project.controller;

import com.project.bean.ProductBean;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService service;

    @RequestMapping("add")
    public String add(ProductBean product, MultipartFile pic) throws Exception{
        String fileName = null;
        if(pic.isEmpty() == false){
            fileName = pic.getOriginalFilename();
            fileName = System.currentTimeMillis() +
                    fileName.substring(fileName.lastIndexOf("."));
            pic.transferTo(new File("E:/J193WEB/upload/productImg/"+fileName));
        }

        product.setImgPath(fileName);
        service.add(product);

        return "ok";
    }

    @RequestMapping("findAll")
    public List<ProductBean> findAll(){
        return service.findAll();
    }

    @RequestMapping("findById")
    public ProductBean findById(Integer id){
        return service.findById(id);
    }
}
